#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QMouseEvent>
#include <QDesktopWidget>
#include <QGraphicsDropShadowEffect>
#include <QProcess>
#include <QBitmap>
#include <QFontMetrics>
QProcess run;
MainWindow::MainWindow(DMainWindow *parent) :
    DMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    titlebar()->setMenuVisible(false);
    titlebar()->setFixedHeight(0);
    this->setFixedSize(200, 200);

    ui->playandpause->setFlat(true);
    ui->skipback->setFlat(true);
    ui->skipforward->setFlat(true);

    ui->label->setStyleSheet("border:1px;border-radius:5px;");
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateUi())); // ***就是你所说的响应函数
    timer->start(1000); // 每隔1s

    //让程序适配浅色模式和深色模式 Adapt the program to light and dark model
    connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged,this,&MainWindow::setTheme);
    updateUi();

    connect(ui->playandpause,&QPushButton::clicked,[=]()
    {
        run.start("playerctl play-pause");
        run.waitForFinished();
        updateUi();
    });
    connect(ui->skipback,&QPushButton::clicked,[=]()
    {
        run.start("playerctl previous");
        run.waitForFinished();
        updateUi();
    });
    connect(ui->skipforward,&QPushButton::clicked,[=]()
    {
        run.start("playerctl next");
        run.waitForFinished();
        updateUi();
    });
}
bool isdark;

void MainWindow::updateUi()
{
    run.start("playerctl metadata mpris:artUrl");
    run.waitForFinished();
    QString output;
    output=run.readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    output.remove("file://");
    QPixmap pix(output);

    // 画成圆形图片
    int width = ui->label->width();
    int height = ui->label->height();
    QSize size(width, height);
    QBitmap mask(size);
    QPainter painter(&mask);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
    painter.setBrush(QColor(0, 0, 0));
    painter.drawRoundedRect(0, 0, size.width(), size.height(), 5, 5);//修改这个值，可以改弧度，和直径相等就是圆形
    QPixmap image = pix.scaled(size);
    image.setMask(mask);
    ui->label->setPixmap(image);

    run.start("playerctl metadata xesam:title");
    run.waitForFinished();
    output=run.readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    QFont font = ui->title->font();	//获取控件的字体
    QFontMetrics fontMetrics(font);		//初始化字体规格
    int fontsize = fontMetrics.width(output);	//获取文字宽度
    output = fontMetrics.elidedText(output, Qt::ElideRight, 140);
    ui->title->setText(output);

    run.start("playerctl metadata xesam:artist");
    run.waitForFinished();
    output=run.readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    font = ui->artist->font();	//获取控件的字体
    fontsize = fontMetrics.width(output);	//获取文字宽度
    output = fontMetrics.elidedText(output, Qt::ElideRight, 140);
    ui->artist->setText(output);

    run.start("playerctl status");
    run.waitForFinished();
    output=run.readAll();
    output.remove("\n");
    output.remove(" ");
    output.remove("\t");
    QIcon bicon;
    if(output!="Paused")
    {
        if(isdark==false)
        {
            bicon.addFile(":/icon-light/res/icon/light/pause.svg");
            ui->playandpause->setIcon(bicon);
        }else {
            bicon.addFile(":/icon-dark/res/icon/dark/pause.svg");
            ui->playandpause->setIcon(bicon);
        }
    }else {
        if(isdark==false)
        {
            bicon.addFile(":/icon-light/res/icon/light/play.svg");
            ui->playandpause->setIcon(bicon);
        }else {
                bicon.addFile(":/icon-dark/res/icon/dark/play.svg");
                ui->playandpause->setIcon(bicon);
        }
    }
}
void QWidget::mouseMoveEvent ( QMouseEvent * event )
{
    this->move(this->x(),this->y());//阻止用户移动小部件
}
void MainWindow::setTheme(DGuiApplicationHelper::ColorType theme)
{
    //这个函数在程序运行之初就会被调用一次，而后每次更换主题颜色时被调用。 This function is called at the beginning of the program and then every time the theme color is changed.
    if(theme==DGuiApplicationHelper::LightType){ //在这个位置设置两种不同的样式 Set two different styles at this location
        //浅色模式 Light Mode
        ui->title->setStyleSheet("color:rgb(0,0,0)");
        QPalette pal(ui->widget->palette());
        pal.setColor(QPalette::Background, Qt::white);
        ui->widget->setAutoFillBackground(true);
        ui->widget->setPalette(pal);
        isdark=false;
        QIcon bicon;
        bicon.addFile(":/icon-light/res/icon/light/skip-back.svg");
        ui->skipback->setIcon(bicon);
        bicon.addFile(":/icon-light/res/icon/light/skip-forward.svg");
        ui->skipforward->setIcon(bicon);
        updateUi();
        qDebug()<<"Lighe";
    }else {
        //深色模式 Dark Mode
        ui->title->setStyleSheet("color:rgb(255,255,255)");
        QPalette pal(ui->widget->palette());
        pal.setColor(QPalette::Background, Qt::black);
        ui->widget->setAutoFillBackground(true);
        ui->widget->setPalette(pal);
        isdark=true;
        QIcon bicon;
        bicon.addFile(":/icon-dark/res/icon/dark/skip-back.svg");
        ui->skipback->setIcon(bicon);
        bicon.addFile(":/icon-dark/res/icon/dark/skip-forward.svg");
        ui->skipforward->setIcon(bicon);
        updateUi();
        qDebug()<<"Dark";
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
